﻿(function () {

    window.testForPartal = {};

    window.testForPartal.initWindows = function (count) {

        var lastLeft = -1, lastTop = -1, tempTotal = count;
        var tempArray = [];
        for (var k = 0; k < tempTotal; k++) {
            tempArray.push(k + 1);
        }

        //找下一个
        var findNextIndex = function (thisIndex) {
            var resultIndex = -1;
            for (var m = 0; m < tempArray.length; m++) {
                if (tempArray[m] == thisIndex && m != tempArray.length - 1) {
                    resultIndex = tempArray[m + 1];
                    break;
                }
            }
            return resultIndex;
        };

        //检查下一个index有效性，返回下个window对象
        var checkNextIndex = function (nextIndex) {
            if (nextIndex == -1) { return null; }

            var nextWindow = $("#window_" + nextIndex);
            if (!nextWindow.length) { return null; }

            return nextWindow;
        };

        var windowSize = $.util.windowSize();
        var windowCount = tempTotal,
            widthPerWindow = 230, heightPerWindow = 150,
            topNorth = 60, topCenterHead = 26, top = 50 + topNorth + topCenterHead, left = 50, rigthPadding = 50,
            widthSpace = 30, heightSpace = 25,
            centerPanel = $("#centerPanel"),
            windowWidth = centerPanel.panel("options").width,
            windowCountPerRow = parseInt(String((parseInt(String(windowWidth)) - left - rigthPadding + widthSpace) / (widthPerWindow + widthSpace))),
            windowRowCount = windowCount / windowCountPerRow + (windowCount % windowCountPerRow == 0 ? 0 : 1);
        //console.log("每行数：" + windowCountPerRow);
        for (var index = 0; index < windowCount; index++) {
            var order = index + 1;
            //console.log("序号：" + order);
            //每行的列号
            var colOrder = order % windowCountPerRow;
            if (colOrder == 0) { colOrder = windowCountPerRow; }
            //console.log("本行序号：" + colOrder);
            //前面的行数
            var rowBeforeCount = order <= windowCountPerRow ? 0 : (order % windowCountPerRow == 0 ? parseInt(order / windowCountPerRow) - 1 : parseInt(order / windowCountPerRow));
            //console.log("前面行数：" + rowBeforeCount);
            var itemTop = top + (rowBeforeCount * (heightPerWindow + heightSpace));
            var itemLeft = left + ((colOrder - 1) * (widthPerWindow + widthSpace));
            //console.log("************************");
            $("<div />").appendTo(centerPanel).window({
                selfIndex: order,
                id: "window_" + order,
                title: order + "号窗口",
                width: widthPerWindow,
                height: heightPerWindow,
                left: itemLeft,
                top: itemTop,
                content: order + "号窗口的内容",
                onClose: function () {
                    var w = $(this), opts = w.window("options"), windowIndex = opts.selfIndex;
                    //console.log("关闭第" + windowIndex + "个window");

                    //找下一个
                    var nextIndex = findNextIndex(windowIndex);

                    //移除windowIndex
                    for (var m = 0; m < tempArray.length; m++) {
                        if (tempArray[m] == windowIndex) {
                            $.array.removeAt(tempArray, m);
                        }
                    }
                    var nextWindow = checkNextIndex(nextIndex);
                    if (!nextWindow) { return; }

                    var nextOpts = nextWindow.window("options")
                    if (nextOpts.closed) { return null; }

                    var thisLeft = opts.left, thisTop = opts.top;
                    lastLeft = nextOpts.left; lastTop = nextOpts.top;

                    //console.log("第" + nextIndex + "个window开始移动");
                    nextWindow.window("move", { top: thisTop, left: thisLeft });
                },
                onMove: function (l, t) {
                    var state = $.data(this, "window"), opts = state.options, windowIndex = opts.selfIndex;
                    //console.log("第" + windowIndex + "个window完成移动");
                    if (!opts.firstMove) { opts.firstMove = true; return; }

                    //找下一个
                    var nextIndex = findNextIndex(windowIndex);
                    var nextWindow = checkNextIndex(nextIndex);
                    if (!nextWindow) { return; }

                    var nextOpts = nextWindow.window("options")
                    if (nextOpts.closed) { return null; }

                    var tempLeft = lastLeft, tempTop = lastTop;
                    lastLeft = nextOpts.left; lastTop = nextOpts.top;
                    //console.log("第" + nextIndex + "个window开始移动");
                    nextWindow.window("move", { top: tempTop, left: tempLeft });
                }
            });
        }
        var centerHeight = centerPanel.height(), centerWith = centerPanel.width(), windowTotalHeight = windowRowCount * (heightPerWindow + heightSpace) + 50 - heightSpace;
        if (windowTotalHeight > centerHeight) {
            console.log(windowTotalHeight);
            //此处应该用新的高度去设置center的高度，但是无效。因为window的postion是绝对值，并不在center的div中。
            //$("body").css({ height: windowTotalHeight });
            //centerPanel.panel("resize", { height: windowTotalHeight, width: centerWith });
            //centerPanel.panel("options").height = windowTotalHeight;
            //$("#thelayout").layout("resize");
        }
    };



})(jQuery);